/**
 *
 * Copyright (C) 2008  Igor Vdovichenko
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Though a sincere effort has been made to deliver a professional,
 * quality product,the library itself is distributed WITHOUT ANY WARRANTY;
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 */
package com.vin.scriptutils.utils.ssh;

import com.vin.scriptutils.utils.UnixCommandBuilder;
import com.vin.scriptutils.utils.ssh.exception.SshTaskExecException;
import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author igor
 */
public class SshCommand {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private SshTaskExec sshTaskExec;

    public SshCommand(SshTaskExec sshTaskExec) {
        this.sshTaskExec = sshTaskExec;
    }

    public SshTaskExec getSshTaskExec() {
        return this.sshTaskExec;
    }

    public void setSshTaskExec(SshTaskExec sshTaskExec) {
        this.sshTaskExec = sshTaskExec;
    }

    public ResultReader execute(SshTask task) throws IOException, SshTaskExecException, InterruptedException {
        SshTaskResult sshTask = this.sshTaskExec.execute(task);
        ResultReader result = new ResultReader().processResult(sshTask);
        return result;
    }

    public ResultReader execute(final String command) throws IOException, SshTaskExecException, InterruptedException {

        SshTaskResult sshTask = this.sshTaskExec.execute(new SshTask() {

            public String getCommand() {
                return command;
            }

            public InputStream getInputStream() throws IOException {
                return null;
            }
        });
        ResultReader result = new ResultReader().processResult(sshTask);
        return result;
    }

    public ResultReader execute(final String command, final InputStream input) throws IOException, SshTaskExecException, InterruptedException {

        SshTaskResult sshTask = this.sshTaskExec.execute(new SshTask() {

            public String getCommand() {
                return command;
            }

            public InputStream getInputStream() throws IOException {
                return input;
            }
        });
        ResultReader result = new ResultReader().processResult(sshTask);
        return result;
    }

    public ResultReader execute(final String command, final ResultReader result) throws IOException, SshTaskExecException, InterruptedException {

        SshTaskResult sshTask = this.sshTaskExec.execute(new SshTask() {

            public String getCommand() {
                return command;
            }

            public InputStream getInputStream() throws IOException {
                return null;
            }
        });
        result.processResult(sshTask);
        return result;
    }

    public ResultReader execute(final String command, final InputStream input, final ResultReader result) throws IOException, SshTaskExecException, InterruptedException {

        SshTaskResult sshTask = this.sshTaskExec.execute(new SshTask() {

            public String getCommand() {
                return command;
            }

            public InputStream getInputStream() throws IOException {
                return input;
            }
        });
        result.processResult(sshTask);
        return result;
    }

    protected ResultReader execute(UnixCommandBuilder unixBuilder) throws IOException, SshTaskExecException, InterruptedException {
        ResultReader rd = execute(unixBuilder.toCommandLine());
        unixBuilder.reset();
        if (rd.isError()) {
            logger.debug(rd.getErrorOutput());
        }
        return rd;
    }
}
